package com.jeeplus.modules.web.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.sys.entity.DictValue;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.DictUtils;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.web.entity.WxAgents;
import com.jeeplus.modules.web.entity.WxUserCoupon;
import com.jeeplus.modules.web.entity.YjEquipment;
import com.jeeplus.modules.web.entity.YjCommissionRatio;
import com.jeeplus.modules.web.mapper.WxAgentsMapper;
import com.jeeplus.modules.web.mapper.YjEquipmentMapper;
import com.jeeplus.modules.web.service.WxEquipmentAddressService;
import com.jeeplus.modules.web.service.YjEquipmentService;
import com.jeeplus.modules.web.service.YjCommissionRatioService;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

@Controller
@RequestMapping(value = "${adminPath}/web/yjCommissionRatio")
public class YjCommissionRatioController extends BaseController {
    @Autowired
    private YjCommissionRatioService yjCommissionRatioService;
    @Autowired
    private WxEquipmentAddressService wxEquipmentAddressService;
    @Autowired
    private WxAgentsMapper wxAgentsMapper;
    @Autowired
    private YjEquipmentService yjEquipmentService;
    @Autowired
    private YjEquipmentMapper yjEquipmentMapper;


    @ModelAttribute
    public YjCommissionRatio get(@RequestParam(required = false) String id) {
        YjCommissionRatio entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = yjCommissionRatioService.get(id);
        }
        if (entity == null) {
            entity = new YjCommissionRatio();
        }
        return entity;
    }

    /**
     * 设备维护信息列表页面
     */
    @RequiresPermissions("web:yjCommissionRatio:list")
    @RequestMapping(value = { "list", "" })
    public String list(Model model) {
        List<DictValue> deviceType = DictUtils.getDictList("deviceType");
        model.addAttribute("deviceType", JSONObject.toJSONString(deviceType));
        return "modules/web/yjCommissionRatioList";
    }

    /**
     * 设备维护信息列表数据 包括1.直接绑定于自己的设备维护，2.其下属代理或门店绑定的设备维护
     */
    @ResponseBody
    @RequiresPermissions("web:yjCommissionRatio:list")
    @RequestMapping(value = "data")
    public Map<String, Object> data(YjCommissionRatio yjCommissionRatio, HttpServletRequest request, HttpServletResponse response,
                                    Model model) {
        Page<YjCommissionRatio> page = yjCommissionRatioService.findPage(new Page<YjCommissionRatio>(request, response), yjCommissionRatio);
        return getBootstrapData(page);
    }


    /**
     * 查看，增加，编辑设备信息表单页面
     */
    @RequiresPermissions(value = { "web:yjCommissionRatio:view", "web:yjCommissionRatio:add",
            "web:yjCommissionRatio:edit" }, logical = Logical.OR)
    @RequestMapping(value = "form")
    public String form(YjCommissionRatio yjCommissionRatio, Model model) {
        List<DictValue> deviceType = DictUtils.getDictList("deviceType");
        model.addAttribute("deviceType", deviceType);
        if(StringUtils.isNoneBlank(yjCommissionRatio.getConfig())){
            yjCommissionRatio.setConfig(StringEscapeUtils.unescapeHtml4(yjCommissionRatio.getConfig().trim()));
        }
        model.addAttribute("yjCommissionRatio", yjCommissionRatio);
        if(StringUtils.isBlank(yjCommissionRatio.getId())){//如果ID是空为添加
            model.addAttribute("isAdd", true);
        }
        return "modules/web/yjCommissionRatioForm";
    }

    /**
     * 保存设备信息
     */
    @RequiresPermissions(value = { "web:yjCommissionRatio:add", "web:yjCommissionRatio:edit" }, logical = Logical.OR)
    @RequestMapping(value = "save")
    public String save(YjCommissionRatio yjCommissionRatio, RedirectAttributes redirectAttributes, Integer pageIndex) throws Exception {
        // 新增或编辑表单保存
        yjCommissionRatioService.save(yjCommissionRatio);// 保存

        addMessage(redirectAttributes, "保存信息成功");
        return "redirect:" + Global.getAdminPath() + "/web/yjCommissionRatio/?repage&maintenance=1&pageIndex="+pageIndex;
    }
    /**
     * 删除用户优惠卷
     */
    @ResponseBody
    @RequiresPermissions("web:yjCommissionRatio:del")
    @RequestMapping(value = "delete")
    public AjaxJson delete(YjCommissionRatio yjCommissionRatio, RedirectAttributes redirectAttributes) {
        AjaxJson j = new AjaxJson();
        yjCommissionRatioService.delete(yjCommissionRatio);
        j.setMsg("删除用户优惠卷成功");
        return j;
    }

    /**
     * 批量删除用户优惠卷
     */
    @ResponseBody
    @RequiresPermissions("web:yjCommissionRatio:del")
    @RequestMapping(value = "deleteAll")
    public AjaxJson deleteAll(String ids, RedirectAttributes redirectAttributes) {
        AjaxJson j = new AjaxJson();
        String idArray[] =ids.split(",");
        for(String id : idArray){
            yjCommissionRatioService.delete(yjCommissionRatioService.get(id));
        }
        j.setMsg("删除用户优惠卷成功");
        return j;
    }

}
